import { NextResponse } from 'next/server';
import { getLocationUrl } from './header';

export function redirectToLogin(returnUrl?: string) {
  const backUrl = returnUrl || getLocationUrl();
  const oUrl = new URL(backUrl);
  const origin = oUrl.origin;
  const url =
    process.env.NEXT_PUBLIC_LOGIN_URL +
    '?return_url=' +
    encodeURIComponent(origin + '/api/login?backurl=' + backUrl);

  return NextResponse.redirect(url);
}
